//package com.yuxi.zk;
//
//import org.apache.curator.framework.CuratorFramework;
//import org.apache.curator.framework.CuratorFrameworkFactory;
//import org.apache.curator.framework.api.CuratorWatcher;
//import org.apache.curator.framework.state.ConnectionState;
//import org.apache.curator.framework.state.ConnectionStateListener;
//import org.apache.curator.retry.RetryNTimes;
//import org.apache.zookeeper.WatchedEvent;
//import org.apache.zookeeper.Watcher;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.concurrent.CountDownLatch;
//
///**
// * Created by yuxi on 16/11/20.
// */
//public class PublishTest {
//
//    private static Logger logger = LoggerFactory.getLogger(PublishTest.class);
//    static CuratorFramework client = null;
//    static final String PATH = "/app1/database_config";
//    static final String zkAddress = "127.0.0.1:2181";
//    static final int timeout = 10000;
//    static CountDownLatch countDownLatch = new CountDownLatch(1);
//    // 客户端的监听配置,有重试机制
//    static ConnectionStateListener clientListener = new ConnectionStateListener() {
//
//        public void stateChanged(CuratorFramework client,
//                                 ConnectionState newState) {
//            if (newState == ConnectionState.CONNECTED) {
//                logger.info("connected established");
//                countDownLatch.countDown();
//            } else if (newState == ConnectionState.LOST) {
//                logger.info("connection lost,waiting for reconection");
//                try {
//                    logger.info("reinit---");
//                    reinit();
//                    logger.info("inited---");
//                } catch (Exception e) {
//                    logger.error("re-inited failed");
//                }
//            }
//
//        }
//    };
//
//    public static void main(String[] args) throws Exception {
//        init();
//        //对path进行监听配置
//        watcherPath(PATH, pathWatcher);
//        Thread.sleep(1000);
//    }
//
//    public static void init() throws Exception {
//        //建立客户端
//        client = CuratorFrameworkFactory.builder().connectString(zkAddress)
//                .sessionTimeoutMs(timeout)
//                .retryPolicy(new RetryNTimes(5, 5000)).build();
//        // 客户端注册监听，进行连接配置
//        client.getConnectionStateListenable().addListener(clientListener);
//        client.start();
//        // 连接成功后，才进行下一步的操作
//        countDownLatch.await();
//        if (null == client.getData().forPath(PATH))
//            client.create().creatingParentsIfNeeded().forPath(PATH, "yuxi understand".getBytes());
//    }
//
//    //关闭,重新注册
//    public static void reinit() {
//        try {
//            unregister();
//            init();
//        } catch (Exception e) {
//        }
//    }
//
//    //关闭链接
//    public static void unregister() {
//        try {
//            if (client != null) {
//                client.close();
//                client = null;
//            }
//        } catch (Exception e) {
//            logger.info("unregister failed");
//        }
//    }
//
//    // 对path进行监听配置
//    public static String watcherPath(String path, CuratorWatcher watcher)
//            throws Exception {
//        byte[] buffer = client.getData().usingWatcher(watcher).forPath(path);
//        System.out.println(new String(buffer));
//        return new String(buffer);
//    }
//
//    //读取path数据
//    public static String readPath(String path) throws Exception {
//        byte[] buffer = client.getData().forPath(path);
//        return new String(buffer);
//
//    }
//
//    //监视器,回调函数
//    static CuratorWatcher pathWatcher = new CuratorWatcher() {
//
//        public void process(WatchedEvent event) throws Exception {
//            // 当数据变化后，重新获取数据信息
//            if (event.getType() == Watcher.Event.EventType.NodeDataChanged) {
//                //获取更改后的数据，进行相应的业务处理
//                String value = readPath(event.getPath());
//                System.out.println(value);
//            }
//
//        }
//    };
//}
